/**
 *******************************************************************************
 * @file  hc32f160_tmrb.c
 * @brief This file provides firmware functions to manage the TMRB(TimerB)
 @verbatim
   Change Logs:
   Date             Author          Notes
   2020-11-27       CDT             First version
 @endverbatim
 *******************************************************************************
 * Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
 *
 * This software component is licensed by HDSC under BSD 3-Clause license
 * (the "License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                    opensource.org/licenses/BSD-3-Clause
 *
 *******************************************************************************
 */

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "hc32f160_tmrb.h"
#include "hc32f160_utility.h"

/**
 * @addtogroup HC32F160_DDL_Driver
 * @{
 */

/**
 * @defgroup DDL_TMRB TMRB
 * @brief TMRB Driver Library
 * @{
 */

#if (DDL_TMRB_ENABLE == DDL_ON)

/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
/**
 * @defgroup TMRB_Local_Macros TMRB Local Macros
 * @{
 */

/**
 * @defgroup TMRB_Check_Parameters_Validity TMRB Check Parameters Validity
 * @{
 */

#define IS_TMRB_UNIT(x)                                                        \
(   ((x) == CM_TMRB_1)                           ||                            \
    ((x) == CM_TMRB_2)                           ||                            \
    ((x) == CM_TMRB_3)                           ||                            \
    ((x) == CM_TMRB_4)                           ||                            \
    ((x) == CM_TMRB_5)                           ||                            \
    ((x) == CM_TMRB_6)                           ||                            \
    ((x) == CM_TMRB_7)                           ||                            \
    ((x) == CM_TMRB_8))

#define IS_TMRB_SYNC_UNIT(x)                                                   \
(   ((x) == CM_TMRB_2)                           ||                            \
    ((x) == CM_TMRB_4)                           ||                            \
    ((x) == CM_TMRB_6)                           ||                            \
    ((x) == CM_TMRB_8))

#define IS_TMRB_CNT_DIR(x)                                                     \
(   ((x) == TMRB_DIR_UP)                        ||                             \
    ((x) == TMRB_DIR_DOWN))

#define IS_TMRB_CNT_MD(x)                                                      \
(   ((x) == TMRB_MD_SAWTOOTH)                   ||                             \
    ((x) == TMRB_MD_TRIANGLE))

#define IS_TMRB_FUNC_MD(x)                                                     \
(   ((x) == TMRB_FUNC_CMP)                      ||                             \
    ((x) == TMRB_FUNC_CAPT))

#define IS_TMRB_CLK_DIV(x)                                                     \
(   ((x) == TMRB_CLK_DIV1)                      ||                             \
    ((x) == TMRB_CLK_DIV2)                      ||                             \
    ((x) == TMRB_CLK_DIV4)                      ||                             \
    ((x) == TMRB_CLK_DIV8)                      ||                             \
    ((x) == TMRB_CLK_DIV16)                     ||                             \
    ((x) == TMRB_CLK_DIV32)                     ||                             \
    ((x) == TMRB_CLK_DIV64)                     ||                             \
    ((x) == TMRB_CLK_DIV128)                    ||                             \
    ((x) == TMRB_CLK_DIV256)                    ||                             \
    ((x) == TMRB_CLK_DIV512)                    ||                             \
    ((x) == TMRB_CLK_DIV1024))

#define IS_TMRB_FLAG(x)                                                        \
(   ((x) != 0U)                                 &&                             \
    (((x) | TMRB_FLAG_ALL) == TMRB_FLAG_ALL))

#define IS_TMRB_INT(x)                                                         \
(   ((x) != 0U)                                 &&                             \
    (((x) | TMRB_INT_ALL) == TMRB_INT_ALL))

#define IS_TMRB_EVT_MXCH(x)                                                    \
(   ((x) != 0U)                                 &&                             \
    (((x) | TMRB_EVT_ALL) == TMRB_EVT_ALL))

#define IS_TMRB_FILTER_CLK_DIV(x)                                              \
(   ((x) == TMRB_FILTER_CLK_DIV1)               ||                             \
    ((x) == TMRB_FILTER_CLK_DIV4)               ||                             \
    ((x) == TMRB_FILTER_CLK_DIV16)              ||                             \
    ((x) == TMRB_FILTER_CLK_DIV64))

#define IS_TMRB_PWM_CNT_STAT(x)                                                \
(   ((x) == TMRB_PWM_CNT_START)                 ||                             \
    ((x) == TMRB_PWM_CNT_STOP)                  ||                             \
    ((x) == TMRB_PWM_CNT_MATCH)                 ||                             \
    ((x) == TMRB_PWM_CNT_PERIOD))

#define IS_TMRB_PWM_START_POLARITY(x)                                          \
(   ((x) == TMRB_PWM_LOW)                       ||                             \
    ((x) == TMRB_PWM_HIGH)                      ||                             \
    ((x) == TMRB_PWM_HOLD))

#define IS_TMRB_PWM_STOP_POLARITY(x)                                           \
(   ((x) == TMRB_PWM_LOW)                       ||                             \
    ((x) == TMRB_PWM_HIGH)                      ||                             \
    ((x) == TMRB_PWM_HOLD))

#define IS_TMRB_PWM_MATCH_POLARITY(x)                                          \
(   ((x) == TMRB_PWM_LOW)                       ||                             \
    ((x) == TMRB_PWM_HIGH)                      ||                             \
    ((x) == TMRB_PWM_HOLD)                      ||                             \
    ((x) == TMRB_PWM_INVT))

#define IS_TMRB_PWM_PERIOD_POLARITY(x)                                         \
(   ((x) == TMRB_PWM_LOW)                       ||                             \
    ((x) == TMRB_PWM_HIGH)                      ||                             \
    ((x) == TMRB_PWM_HOLD)                      ||                             \
    ((x) == TMRB_PWM_INVT))

#define IS_TMRB_PWM_POLARITY(state, polarity)                                    \
(   (((state) == TMRB_PWM_CNT_START) && IS_TMRB_PWM_START_POLARITY(polarity)) || \
    (((state) == TMRB_PWM_CNT_STOP)  && IS_TMRB_PWM_STOP_POLARITY(polarity))  || \
    (((state) == TMRB_PWM_CNT_MATCH) && IS_TMRB_PWM_MATCH_POLARITY(polarity)) || \
    (((state) == TMRB_PWM_CNT_PERIOD) && IS_TMRB_PWM_PERIOD_POLARITY(polarity)))

#define IS_TMRB_PWM_FORCE_POLARITY(x)                                          \
(   ((x) == TMRB_PWM_FORCE_INVD)                ||                             \
    ((x) == TMRB_PWM_FORCE_LOW)                 ||                             \
    ((x) == TMRB_PWM_FORCE_HIGH))

#define IS_TMRB_CAPT_COND(x)                                                   \
(   ((x) != 0U)                                 ||                             \
    (((x) | TMRB_START_COND_ALL) == TMRB_START_COND_ALL))

#define IS_TMRB_START_COND(x)                                                  \
(   ((x) != 0U)                                 ||                             \
    (((x) | TMRB_START_COND_ALL) == TMRB_START_COND_ALL))

#define IS_TMRB_STOP_COND(x)                                                   \
(   ((x) != 0U)                                 ||                             \
    (((x) | TMRB_STOP_COND_ALL) == TMRB_STOP_COND_ALL))

#define IS_TMRB_CLR_COND(x)                                                    \
(   ((x) != 0U)                                 ||                             \
    (((x) | TMRB_CLR_COND_ALL) == TMRB_CLR_COND_ALL))

#define IS_TMRB_CNT_UP_COND(x)                                                 \
(   ((x) != 0U)                                 ||                             \
    (((x) | TMRB_CNT_UP_COND_ALL) == TMRB_CNT_UP_COND_ALL))

#define IS_TMRB_CNT_DOWN_COND(x)                                               \
(   ((x) != 0U)                                 ||                             \
    (((x) | TMRB_CNT_DOWN_COND_ALL) == TMRB_CNT_DOWN_COND_ALL))

/**
 * @}
 */

/**
 * @defgroup TMRB_Flag_Mask TMRB Flag Mask
 * @{
 */
#define TMRB_CNT_FLAG_MASK                  (TMRB_FLAG_OVF | TMRB_FLAG_UDF)
#define TMRB_CMP_FLAG_MASK                  (TMRB_FLAG_CMP)
/**
 * @}
 */

/**
 * @defgroup TMRB_Interrupt_Mask TMRB Interrupt Mask
 * @{
 */
#define TMRB_CNT_INT_MASK                   (TMRB_INT_OVF | TMRB_INT_UDF)
#define TMRB_CMP_INT_MASK                   (TMRB_INT_CMP)
/**
 * @}
 */

/**
 * @}
 */

/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/

/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
/**
 * @defgroup TMRB_Global_Functions TMRB Global Functions
 * @{
 */

/**
 * @brief  Initialize TMRB base function.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] pstcTmrbInit        Pointer to a @ref stc_tmrb_init_t structure.
 * @retval An en_result_t enumeration value:
 *           - Ok: Initialize success
 *           - ErrorInvalidParameter: pstcTmrbInit is NULL
 */
en_result_t TMRB_Init(CM_TMRB_TypeDef *TMRBx,
                        const stc_tmrb_init_t *pstcTmrbInit)
{
    en_result_t enRet = ErrorInvalidParameter;

    if (NULL != pstcTmrbInit)
    {
        /* Check parameters */
        DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
        DDL_ASSERT(IS_TMRB_CLK_DIV(pstcTmrbInit->u16ClockDiv));
        DDL_ASSERT(IS_TMRB_CNT_MD(pstcTmrbInit->u16CountMode));
        DDL_ASSERT(IS_TMRB_CNT_DIR(pstcTmrbInit->u16CountDir));

        WRITE_REG16(TMRBx->BCSTR, (pstcTmrbInit->u16CountDir | \
                                   pstcTmrbInit->u16ClockDiv | \
                                   pstcTmrbInit->u16CountMode));

        WRITE_REG16(TMRBx->CNTER, 0U);
        WRITE_REG16(TMRBx->PERAR, pstcTmrbInit->u16PeriodValue);
        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Set the fields of structure stc_tmrb_init_t to default values.
 * @param  [out] pstcTmrbInit       Pointer to a @ref stc_tmrb_init_t structure.
 * @retval An en_result_t enumeration value:
 *           - Ok: Initialize success
 *           - ErrorInvalidParameter: pstcTmrbInit is NULL
 */
en_result_t TMRB_StructInit(stc_tmrb_init_t *pstcTmrbInit)
{
    en_result_t enRet = ErrorInvalidParameter;

    /* Check parameters */
    if (NULL != pstcTmrbInit)
    {
        pstcTmrbInit->u16ClockDiv = TMRB_CLK_DIV1;
        pstcTmrbInit->u16CountMode = TMRB_MD_SAWTOOTH;
        pstcTmrbInit->u16CountDir = TMRB_DIR_UP;
        pstcTmrbInit->u16PeriodValue = 0xFFFFU;
        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  De-Initialize TMRB unit base function.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @retval None
 */
void TMRB_DeInit(CM_TMRB_TypeDef *TMRBx)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));

    /* Configures the registers to reset value. */
    WRITE_REG16(TMRBx->BCSTR, 0x0002U);
    WRITE_REG16(TMRBx->CNTER, 0x0000U);
    WRITE_REG16(TMRBx->PERAR, 0xFFFFU);
}

/**
 * @brief  Get the TMRB counter count value.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @retval Counter count value (between Min_Data=0 and Max_Data=0xFFFF)
 */
uint16_t TMRB_GetCountValue(const CM_TMRB_TypeDef *TMRBx)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));

    return READ_REG16(TMRBx->CNTER);
}

/**
 * @brief  Set the TMRB counter count value.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Value            Counter count value (between Min_Data=0 and Max_Data=0xFFFF)
 * @retval None
 */
void TMRB_SetCountValue(CM_TMRB_TypeDef *TMRBx, uint16_t u16Value)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));

    WRITE_REG16(TMRBx->CNTER, u16Value);
}

/**
 * @brief  Get TMRB counter period value.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @retval Counter period value (between Min_Data=0 and Max_Data=0xFFFF)
 */
uint16_t TMRB_GetPeriodValue(const CM_TMRB_TypeDef *TMRBx)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));

    return READ_REG16(TMRBx->PERAR);
}

/**
 * @brief  Set TMRB counter period value.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Value            Counter period value (between Min_Data=0 and Max_Data=0xFFFF)
 * @retval None
 */
void TMRB_SetPeriodValue(CM_TMRB_TypeDef *TMRBx, uint16_t u16Value)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));

    WRITE_REG16(TMRBx->PERAR, u16Value);
}

/**
 * @brief  Get TMRB compare register value.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @retval Compare value (between Min_Data=0 and Max_Data=0xFFFF)
 */
uint16_t TMRB_GetCompareValue(const CM_TMRB_TypeDef *TMRBx)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));

    return READ_REG16(TMRBx->CMPAR);
}

/**
 * @brief  Set TMRB compare register value.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Value            Compare value (between Min_Data=0 and Max_Data=0xFFFF)
 * @retval None
 */
void TMRB_SetCompareValue(CM_TMRB_TypeDef *TMRBx,
                                uint16_t u16Value)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));

    WRITE_REG16(TMRBx->CMPAR, u16Value);
}

/**
 * @brief  Start TMRB counter.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @retval None
 */
void TMRB_Start(CM_TMRB_TypeDef *TMRBx)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));

    SET_REG16_BIT(TMRBx->BCSTR, TMRB_BCSTR_START);
}

/**
 * @brief  Stop TMRB counter.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @retval None
 */
void TMRB_Stop(CM_TMRB_TypeDef *TMRBx)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));

    CLEAR_REG16_BIT(TMRBx->BCSTR, TMRB_BCSTR_START);
}

/**
 * @brief  Enable or disable TMRB sync start.
 * @param  [in] TMRBx                   Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_2:            TMRB unit 2 instance register base
 *           @arg CM_TMRB_4:            TMRB unit 4 instance register base
 *           @arg CM_TMRB_6:            TMRB unit 6 instance register base
 *           @arg CM_TMRB_8:            TMRB unit 8 instance register base
 * @param  [in] enNewState              The function new state
 *           @arg This parameter can be: Enable or Disable.
 * @retval None
 * @note   The function is only valid for TMRB_<m>(m=2/4/6/8).
 */
void TMRB_SyncStartCmd(CM_TMRB_TypeDef *TMRBx,
                        en_functional_state_t enNewState)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_SYNC_UNIT(TMRBx));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    if (Enable == enNewState)
    {
        SET_REG16_BIT(TMRBx->BCSTR, TMRB_BCSTR_SYNST);
    }
    else
    {
        CLEAR_REG16_BIT(TMRBx->BCSTR, TMRB_BCSTR_SYNST);
    }
}

/**
 * @brief  Get TMRB flag status.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Flag             TMRB flag type
 *         This parameter can be any composed value of the following values:
 *           @arg TMRB_FLAG_OVF:    Overflow flag
 *           @arg TMRB_FLAG_UDF:    Underflow flag
 *           @arg TMRB_FLAG_CMP:    Compare match flag
 * @retval An en_flag_status_t enumeration value:
 *           - Set:   Any bit of the composed flag is set.
 *           - Reset: All bit of the composed flag is reset.
 */
en_flag_status_t TMRB_GetStatus(const CM_TMRB_TypeDef *TMRBx,
                                uint16_t u16Flag)
{
    en_flag_status_t enCntStatus = Reset;
    en_flag_status_t enCmpStatus = Reset;
    const uint16_t u16CntFlag = (u16Flag & TMRB_CNT_FLAG_MASK);
    const uint16_t u16CmpFlag = (u16Flag & TMRB_CMP_FLAG_MASK);

    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_FLAG(u16Flag));

    if (u16CntFlag == READ_REG16_BIT(TMRBx->BCSTR, u16CntFlag))
    {
        enCntStatus = Set;
    }

    if (u16CmpFlag == READ_REG16_BIT(TMRBx->STFLR, u16CmpFlag))
    {
        enCmpStatus = Set;
    }

    return ((Set == enCntStatus) || (Set == enCmpStatus)) ? Set : Reset;
}

/**
 * @brief  Clear TMRB flag.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Flag             TMRB flag type
 *         This parameter can be any composed value of the following values:
 *           @arg TMRB_FLAG_OVF:    Overflow flag
 *           @arg TMRB_FLAG_UDF:    Underflow flag
 *           @arg TMRB_FLAG_CMP:    Compare match flag
 * @retval None
 */
void TMRB_ClearStatus(CM_TMRB_TypeDef *TMRBx, uint16_t u16Flag)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_FLAG(u16Flag));

    if (0U != (u16Flag & TMRB_CNT_FLAG_MASK))
    {
        CLEAR_REG16_BIT(TMRBx->BCSTR, (u16Flag & TMRB_CNT_FLAG_MASK));
    }

    if (0U != (u16Flag & TMRB_CMP_FLAG_MASK))
    {
        CLEAR_REG16_BIT(TMRBx->STFLR, (u16Flag & TMRB_CMP_FLAG_MASK));
    }
}

/**
 * @brief  Set TMRB clock division.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Div              TMRB clock division
 *         This parameter can be one of the following values:
 *           @arg TMRB_CLK_DIV1:    HCLK
 *           @arg TMRB_CLK_DIV2:    HCLK/2
 *           @arg TMRB_CLK_DIV4:    HCLK/4
 *           @arg TMRB_CLK_DIV8:    HCLK/8
 *           @arg TMRB_CLK_DIV16:   HCLK/16
 *           @arg TMRB_CLK_DIV32:   HCLK/32
 *           @arg TMRB_CLK_DIV64:   HCLK/64
 *           @arg TMRB_CLK_DIV128:  HCLK/128
 *           @arg TMRB_CLK_DIV256:  HCLK/256
 *           @arg TMRB_CLK_DIV512:  HCLK/512
 *           @arg TMRB_CLK_DIV1024: HCLK/1024
 * @retval None
 * @note   The CLK division function is valid when clock source is HCLK
 */
void TMRB_SetClockDiv(CM_TMRB_TypeDef *TMRBx, uint16_t u16Div)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_CLK_DIV(u16Div));

    MODIFY_REG16(TMRBx->BCSTR, TMRB_BCSTR_CKDIV, u16Div);
}

/**
 * @brief  Set the TMRB count direction.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Dir              The TMRB count direction
 *         This parameter can be one of the following values:
 *           @arg TMRB_DIR_DOWN:    Count down
 *           @arg TMRB_DIR_UP:      Count up
 * @retval None
 */
void TMRB_SetCountDir(CM_TMRB_TypeDef *TMRBx, uint16_t u16Dir)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_CNT_DIR(u16Dir));

    MODIFY_REG16(TMRBx->BCSTR, TMRB_BCSTR_DIR, u16Dir);
}

/**
 * @brief  Get the TMRB count direction.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @retval Count direction:
 *           - TMRB_DIR_DOWN:       Count down
 *           - TMRB_DIR_UP:         Count up
 */
uint16_t TMRB_GetCountDir(const CM_TMRB_TypeDef *TMRBx)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));

    return READ_REG16_BIT(TMRBx->BCSTR, TMRB_BCSTR_DIR);
}

/**
 * @brief  Set the TMRB count mode.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Mode             The TMRB count mode
 *         This parameter can be one of the following values:
 *           @arg TMRB_MD_SAWTOOTH: Sawtooth wave mode
 *           @arg TMRB_MD_TRIANGLE: Triangle wave mode
 * @retval None
 */
void TMRB_SetCountMode(CM_TMRB_TypeDef *TMRBx, uint16_t u16Mode)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_CNT_MD(u16Mode));

    MODIFY_REG16(TMRBx->BCSTR, TMRB_BCSTR_MODE, u16Mode);
}

/**
 * @brief  Enable or disable the specified TMRB interrupt.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16IntType          TMRB interrupt type
 *         This parameter can be any composed value of the following values:
 *           @arg TMRB_INT_OVF:     TMRB Overflow interrupt
 *           @arg TMRB_INT_UDF:     TMRB Underflow interrupt
 *           @arg TMRB_INT_CMP:     TMRB compare channel 1 match interrupt
 * @param  [in] enNewState          The function new state.
 *         This parameter can be one of the following values:
 *           @arg Enable:           Enable the specified TMRB interrupt.
 *           @arg Disable:          Disable the specified TMRB interrupt.
 * @retval None
 */
void TMRB_IntCmd(CM_TMRB_TypeDef *TMRBx,
                    uint16_t u16IntType,
                    en_functional_state_t enNewState)
{
    const uint16_t u16CntIntType = (u16IntType & TMRB_CNT_INT_MASK);
    const uint16_t u16CmpIntType = (u16IntType & TMRB_CMP_INT_MASK);

    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_INT(u16IntType));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    if (Enable == enNewState)
    {
        SET_REG16_BIT(TMRBx->BCSTR, u16CntIntType);
        SET_REG16_BIT(TMRBx->ICONR, u16CmpIntType);
    }
    else
    {
        CLEAR_REG16_BIT(TMRBx->BCSTR, u16CntIntType);
        CLEAR_REG16_BIT(TMRBx->ICONR, u16CmpIntType);
    }
}

/**
 * @brief  Enable or disable the specified compare channel event of TMRB.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] enNewState          The function new state.
 *           @arg Enable:  Enable the specified channel event.
 *           @arg Disable: Disable the specified channel event.
 * @retval None
 */
void TMRB_EventCmd(CM_TMRB_TypeDef *TMRBx,
                        en_functional_state_t enNewState)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    if (Enable == enNewState)
    {
        SET_REG16_BIT(TMRBx->ECONR, TMRB_ECONR_ETEN1);
    }
    else
    {
        CLEAR_REG16_BIT(TMRBx->ECONR, TMRB_ECONR_ETEN1);
    }
}

/**
 * @brief  Set TMRB counter period value.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Func             Function mode
 *         This parameter can be one of the following values:
 *           @arg TMRB_FUNC_CMP:    Compare output function
 *           @arg TMRB_FUNC_CAPT:   Capture input function
 * @retval None
 */
void TMRB_SetFunc(CM_TMRB_TypeDef *TMRBx, uint16_t u16Func)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_FUNC_MD(u16Func));

    MODIFY_REG16(TMRBx->CCONR, TMRB_CCONR_CAPMD, u16Func);
}

/**
 * @brief  Enable or disable TMRB reload function.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] enNewState          The function new state.
 *         This parameter can be one of the following values:
 *           @arg Enable:           Enable the reload function.
 *           @arg Disable:          Disable the reload function.
 * @retval None
 */
void TMRB_ReloadCmd(CM_TMRB_TypeDef *TMRBx,
                        en_functional_state_t enNewState)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    if (Enable == enNewState)
    {
        CLEAR_REG16_BIT(TMRBx->BCSTR, TMRB_BCSTR_OVSTP);
    }
    else
    {
        SET_REG16_BIT(TMRBx->BCSTR, TMRB_BCSTR_OVSTP);
    }
}

/**
 * @brief  Enable or disable the specified hardware start condition of TMRB.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Cond             TMRB hardware start counter condition
 *         This parameter can be any composed value of the following values:
 *           @arg TMRB_START_COND_PWMR: Start count when detect the rising edge on TIMB_<t>_PWM1
 *           @arg TMRB_START_COND_PWMF: Start count when detect the falling edge on TIMB_<t>_PWM1
 *           @arg TMRB_START_COND_EVT:  Start count when detect the timer TMRB_HTSSR specified event
 * @param  [in] enNewState          The function new state
 *         This parameter can be one of the following values:
 *           @arg Enable:           Enable the specified hardware start condition.
 *           @arg Disable:          Disable the specified hardware start condition.
 * @retval None
 */
void TMRB_HWStartCondCmd(CM_TMRB_TypeDef *TMRBx,
                                uint16_t u16Cond,
                                en_functional_state_t enNewState)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_START_COND(u16Cond));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    if (Enable == enNewState)
    {
        SET_REG16_BIT(TMRBx->HCONR, u16Cond);
    }
    else
    {
        CLEAR_REG16_BIT(TMRBx->HCONR, u16Cond);
    }
}

/**
 * @brief  Enable or disable the specified hardware stop condition of TMRB.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Cond             TMRB hardware start counter condition
 *         This parameter can be any composed value of the following values:
 *           @arg TMRB_STOP_COND_PWMR: Stop count when detect the rising edge on TIMB_<t>_PWM1(t=1~8)
 *           @arg TMRB_STOP_COND_PWMF: Stop count when detect the falling edge on TIMB_<t>_PWM1(t=1~8)
 *           @arg TMRB_STOP_COND_EVT:  Stop count when detect the timer TMRB_HTSSR specified event
 * @param  [in] enNewState          The function new state
 *         This parameter can be one of the following values:
 *           @arg Enable:           Enable the specified hardware stop condition.
 *           @arg Disable:          Disable the specified hardware stop condition.
 * @retval None
 */
void TMRB_HWStopCondCmd(CM_TMRB_TypeDef *TMRBx,
                                uint16_t u16Cond,
                                en_functional_state_t enNewState)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_STOP_COND(u16Cond));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    if (Enable == enNewState)
    {
        SET_REG16_BIT(TMRBx->HCONR, u16Cond);
    }
    else
    {
        CLEAR_REG16_BIT(TMRBx->HCONR, u16Cond);
    }
}

/**
 * @brief  Enable or disable the specified hardware clear condition of TMRB.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Cond             TMRB hardware start counter condition
 *         This parameter can be any composed value of the following values:
 *           @arg TMRB_CLR_COND_PWMR: Clear count value when detect the rising edge on TIMB_<t>_PWM1(t=1~8)
 *           @arg TMRB_CLR_COND_PWMF: Clear count value when detect the falling edge on TIMB_<t>_PWM1(t=1~8)
 *           @arg TMRB_CLR_COND_EVT:  Clear count value when detect the timer TMRB_HTSSR specified event
 *           @arg TMRB_CLR_COND_SYM_PWMR: Clear count value when detect the rising edge on TIMB_<n>_PWM1, _<n>_=2/4/6/8 when current unit m=1/3/5/7 or n=1/3/5/7 when current unit m=2/4/6/8
 *           @arg TMRB_CLR_COND_SYM_PWMF: Clear count value when detect the falling edge on TIMB_<n>_PWM1, _<n>_=2/4/6/8 when current unit m=1/3/5/7 or n=1/3/5/7 when current unit m=2/4/6/8
 * @param  [in] enNewState          The function new state
 *         This parameter can be one of the following values:
 *           @arg Enable:           Enable the specified hardware clear condition.
 *           @arg Disable:          Disable the specified hardware clear condition.
 * @retval None
 */
void TMRB_HWClearCondCmd(CM_TMRB_TypeDef *TMRBx,
                                uint16_t u16Cond,
                                en_functional_state_t enNewState)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_CLR_COND(u16Cond));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    if (Enable == enNewState)
    {
        SET_REG16_BIT(TMRBx->HCONR, u16Cond);
    }
    else
    {
        CLEAR_REG16_BIT(TMRBx->HCONR, u16Cond);
    }
}

/**
 * @brief  Enable or disable the specified hardware count up condition of TMRB.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Cond             TMRB hardware up counter condition
 *         This parameter can be any composed value of the following values:
 *           @arg TMRB_UP_COND_PWMR: Count up when detect the rising edge on TIMB_<t>_PWM1(t=1~8)
 *           @arg TMRB_UP_COND_PWMF: Count up when detect the falling edge on TIMB_<t>_PWM1(t=1~8)
 *           @arg TMRB_UP_COND_EVT:  Count up when detect the timer TMRB_HTSSR specified event
 *           @arg TMRB_CNT_UP_COND_SYM_OVF: Count up by overflow of TIMB_<n>, _<n>_=2/4/6/8 when current unit m=1/3/5/7 or n=1/3/5/7 when current unit m=2/4/6/8
 *           @arg TMRB_CNT_UP_COND_SYM_UDF: Count up by underflow of TIMB_<n>, _<n>_=2/4/6/8 when current unit m=1/3/5/7 or n=1/3/5/7 when current unit m=2/4/6/8
 * @param  [in] enNewState          The function new state
 *         This parameter can be one of the following values:
 *           @arg Enable:           Enable the specified hardware count up condition.
 *           @arg Disable:          Disable the specified hardware count up condition.
 * @retval None
 */
void TMRB_HWCountUpCondCmd(CM_TMRB_TypeDef *TMRBx,
                                uint16_t u16Cond,
                                en_functional_state_t enNewState)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_CNT_UP_COND(u16Cond));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    if (Enable == enNewState)
    {
        SET_REG16_BIT(TMRBx->HCUPR, u16Cond);
    }
    else
    {
        CLEAR_REG16_BIT(TMRBx->HCUPR, u16Cond);
    }
}

/**
 * @brief  Enable or disable the specified hardware count down condition of TMRB.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Cond             TMRB hardware down counter condition
 *         This parameter can be any composed value of the following values:
 *           @arg TMRB_DOWN_COND_PWMR: Count down when detect the rising edge on TIMB_<t>_PWM1(t=1~8)
 *           @arg TMRB_DOWN_COND_PWMF: Count down when detect the falling edge on TIMB_<t>_PWM1(t=1~8)
 *           @arg TMRB_DOWN_COND_EVT:  Count down when detect the timer TMRB_HTSSR specified event
 *           @arg TMRB_CNT_DOWN_COND_SYM_OVF: Count down by overflow of TIMB_<n>, _<n>_=2/4/6/8 when current unit m=1/3/5/7 or n=1/3/5/7 when current unit m=2/4/6/8
 *           @arg TMRB_CNT_DOWN_COND_SYM_UDF: Count down by underflow of TIMB_<n>, _<n>_=2/4/6/8 when current unit m=1/3/5/7 or n=1/3/5/7 when current unit m=2/4/6/8
 * @param  [in] enNewState          The function new state
 *         This parameter can be one of the following values:
 *           @arg Enable:           Enable the specified hardware count down condition.
 *           @arg Disable:          Disable the specified hardware count down condition.
 * @retval None
 */
void TMRB_HWCountDownCondCmd(CM_TMRB_TypeDef *TMRBx,
                                uint16_t u16Cond,
                                en_functional_state_t enNewState)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_CNT_DOWN_COND(u16Cond));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    if (Enable == enNewState)
    {
        SET_REG16_BIT(TMRBx->HCDOR, u16Cond);
    }
    else
    {
        CLEAR_REG16_BIT(TMRBx->HCDOR, u16Cond);
    }
}

/**
 * @brief  Enable or disable the specified hardware capture condition of TMRB.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Cond             TMRB hardware down counter condition
 *         This parameter can be any composed value of the following values:
 *           @arg TMRB_CAPT_COND_PWMR: Trigger capture when detect the rising edge on TIMB_<t>_PWMn(n=1~4)
 *           @arg TMRB_CAPT_COND_PWMF: Trigger capture when detect the falling edge on TIMB_<t>_PWMn(n=1~4)
 *           @arg TMRB_CAPT_COND_EVT:  Trigger capture when detect the timer TMRB_HTSSR specified event
 * @param  [in] enNewState          The function new state
 *         This parameter can be one of the following values:
 *           @arg Enable:           Enable the specified hardware capture condition.
 *           @arg Disable:          Disable the specified hardware capture condition.
 * @retval None
 */
void TMRB_HWCaptureCondCmd(CM_TMRB_TypeDef *TMRBx,
                                uint16_t u16Cond,
                                en_functional_state_t enNewState)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_CNT_DOWN_COND(u16Cond));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    if (Enable == enNewState)
    {
        SET_REG16_BIT(TMRBx->CCONR, u16Cond);
    }
    else
    {
        CLEAR_REG16_BIT(TMRBx->CCONR, u16Cond);
    }
}

/**
 * @brief  Enable or disalbe the TMRB input capture noise filter function.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] enNewState          The function new state.
 *         This parameter can be one of the following values:
 *           @arg Enable:           Enable the TMRB input capture noise filter function.
 *           @arg Disable:          Disable the TMRB input capture noise filter function.
 * @retval None
 */
void TMRB_FilterCmd(CM_TMRB_TypeDef *TMRBx,
                    en_functional_state_t enNewState)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));

    if (Enable == enNewState)
    {
        SET_REG16_BIT(TMRBx->CCONR, TMRB_CCONR_NOFIENCP);
    }
    else
    {
        CLEAR_REG16_BIT(TMRBx->CCONR, TMRB_CCONR_NOFIENCP);
    }
}

/**
 * @brief  Set TMRB input capture noise filter clock division.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Div              TMRB input capture noise filter clock division
 *         This parameter can be one of the following values:
 *           @arg TMRB_FILTER_CLK_DIV1:  HCLK
 *           @arg TMRB_FILTER_CLK_DIV4:  HCLK/4
 *           @arg TMRB_FILTER_CLK_DIV16: HCLK/16
 *           @arg TMRB_FILTER_CLK_DIV64: HCLK/64
 * @retval None
 */
void TMRB_SetFilterClockDiv(CM_TMRB_TypeDef *TMRBx,
                                    uint16_t u16Div)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_FILTER_CLK_DIV(u16Div));

    MODIFY_REG16(TMRBx->CCONR, TMRB_CCONR_NOFICKCP, u16Div);
}

/**
 * @brief  Set the specified event trigger source for TMRB.
 * @param  [in] enEventSrc          The trigger external event source source.
 *           @arg This parameter can be any value of @ref en_event_src_t
 * @retval None
 */
void TMRB_SetTriggerSrc(en_event_src_t enEventSrc)
{
    WRITE_REG32(CM_AOS->TMRB_HTSSR, ((uint32_t)enEventSrc) & 0x1FFUL);
}

/**
 * @defgroup TMRB_PWM_Global_Functions TMRB PWM Global Functions
 * @{
 */

/**
 * @brief  Initialize TMRB PWM function.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] pstcPwmInit         Pointer to a @ref stc_tmrb_pwm_init_t.
 * @retval An en_result_t enumeration value:
 *           - Ok: Initialize success
 *           - ErrorInvalidParameter: pstcPwmInit is NULL
 */
en_result_t TMRB_PWM_Init(CM_TMRB_TypeDef *TMRBx,
                            const stc_tmrb_pwm_init_t *pstcPwmInit)
{
    en_result_t enRet = ErrorInvalidParameter;

    if (NULL != pstcPwmInit)
    {
        /* Check parameters */
        DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
        DDL_ASSERT(IS_TMRB_PWM_START_POLARITY(pstcPwmInit->u16StartPolarity));
        DDL_ASSERT(IS_TMRB_PWM_STOP_POLARITY(pstcPwmInit->u16StopPolarity));
        DDL_ASSERT(IS_TMRB_PWM_MATCH_POLARITY(pstcPwmInit->u16MatchPolarity));
        DDL_ASSERT(IS_TMRB_PWM_PERIOD_POLARITY(pstcPwmInit->u16PeriodPolarity));

        /* Set TMRB compare output function mode */
        TMRB_SetFunc(TMRBx, TMRB_FUNC_CMP);

        WRITE_REG16(TMRBx->CMPAR, pstcPwmInit->u16CompareValue);

        WRITE_REG16(TMRBx->PCONR, \
                    ((uint16_t)(pstcPwmInit->u16StartPolarity << TMRB_PCONR_STAC_POS) | \
                     (uint16_t)(pstcPwmInit->u16StopPolarity << TMRB_PCONR_STPC_POS)  | \
                     (uint16_t)(pstcPwmInit->u16MatchPolarity << TMRB_PCONR_CMPC_POS) | \
                     (uint16_t)(pstcPwmInit->u16PeriodPolarity << TMRB_PCONR_PERC_POS)));
        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  Set the fields of structure stc_tmrb_pwm_init_t to default values.
 * @param  [out] pstcPwmInit        Pointer to a @ref stc_tmrb_pwm_init_t structure.
 * @retval An en_result_t enumeration value:
 *           - Ok: Initialize success
 *           - ErrorInvalidParameter: pstcPwmInit is NULL
 */
en_result_t TMRB_PWM_StructInit(stc_tmrb_pwm_init_t *pstcPwmInit)
{
    en_result_t enRet = ErrorInvalidParameter;

    /* Check parameters */
    if (NULL != pstcPwmInit)
    {
        pstcPwmInit->u16CompareValue = 0xFFFFU;
        pstcPwmInit->u16StartPolarity = TMRB_PWM_LOW;
        pstcPwmInit->u16StopPolarity = TMRB_PWM_LOW;
        pstcPwmInit->u16MatchPolarity = TMRB_PWM_LOW;
        pstcPwmInit->u16PeriodPolarity = TMRB_PWM_LOW;
        enRet = Ok;
    }

    return enRet;
}

/**
 * @brief  De-Initialize the TMRB PWM.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @retval None
 */
void TMRB_PWM_DeInit(CM_TMRB_TypeDef *TMRBx)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));

    /* Configures the registers to reset value. */
    WRITE_REG16(TMRBx->PCONR, 0x0000U);
    WRITE_REG16(TMRBx->CMPAR, 0xFFFFU);
    WRITE_REG16(TMRBx->STFLR, 1);
}

/**
 * @brief  Enable or disable the TMRB PWM TIM_<t>_PWM1(t=1~8) port function.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] enNewState          The function new state.
 *         This parameter can be one of the following values:
 *           @arg Enable:           Enable the TMRB PWM TIM_<t>_PWM1(t=1~8) port function.
 *           @arg Disable:          Disable the TMRB PWM TIM_<t>_PWM1(t=1~8) port function.
 * @retval None
 */
void TMRB_PWM_Cmd(CM_TMRB_TypeDef *TMRBx,
                        en_functional_state_t enNewState)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));

    if (Enable == enNewState)
    {
        SET_REG16_BIT(TMRBx->PCONR, TMRB_PCONR_OUTEN);
    }
    else
    {
        CLEAR_REG16_BIT(TMRBx->PCONR, TMRB_PCONR_OUTEN);
    }
}

/**
 * @brief  Set TIM_<t>_PWM1(t=1~8) port output polarity.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16CountState       TMRB count state
 *         This parameter can be one of the following values:
 *           @arg TMRB_PWM_CNT_START:  The TMRB start count
 *           @arg TMRB_PWM_CNT_STOP:   The TMRB stop count
 *           @arg TMRB_PWM_CNT_MATCH:  The TMRB compare match
 *           @arg TMRB_PWM_CNT_PERIOD: The TMRB period match
 * @param  [in] u16Polarity         TMRB TIMB_<t>_PWMn port output polarity
 *         This parameter can be one of the following values:
 *           @arg TMRB_PWM_LOW:  TIM_<t>_PWMn(t=1,n=1~4) output low level
 *           @arg TMRB_PWM_HIGH: TIM_<t>_PWMn(t=1,n=1~4) output high level
 *           @arg TMRB_PWM_HOLD: TIM_<t>_PWMn(t=1,n=1~4) output hold level
 *           @arg TMRB_PWM_INVT: TIM_<t>_PWMn(t=1,n=1~4) output inverted level
 * @retval None
 */
void TMRB_PWM_SetPolarity(CM_TMRB_TypeDef *TMRBx,
                          uint16_t u16CountState,
                          uint16_t u16Polarity)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_PWM_POLARITY(u16CountState, u16Polarity));

    MODIFY_REG16(TMRBx->PCONR, (((uint16_t)3U) << u16CountState), (u16Polarity << u16CountState));
}

/**
 * @brief  Set TIM_<t>_PWM1(t=1~8) port force output polarity.
 * @param  [in] TMRBx               Pointer to TMRB instance register base
 *         This parameter can be one of the following values:
 *           @arg CM_TMRB_1:        TMRB unit 1 instance register base
 *           @arg CM_TMRB_2:        TMRB unit 2 instance register base
 *           @arg CM_TMRB_3:        TMRB unit 3 instance register base
 *           @arg CM_TMRB_4:        TMRB unit 4 instance register base
 *           @arg CM_TMRB_5:        TMRB unit 5 instance register base
 *           @arg CM_TMRB_6:        TMRB unit 6 instance register base
 *           @arg CM_TMRB_7:        TMRB unit 7 instance register base
 *           @arg CM_TMRB_8:        TMRB unit 8 instance register base
 * @param  [in] u16Polarity         TMRB TIMB_<t>_PWMn port output polarity
 *         This parameter can be one of the following values:
 *           @arg TMRB_PWM_FORCE_INVD: Invalid
 *           @arg TMRB_PWM_FORCE_LOW:  Force TIM_<t>_PWM1(t=1~8) output low level
 *           @arg TMRB_PWM_FORCE_HIGH: Force TIM_<t>_PWM1(t=1~8) force output high level
 * @retval None
 */
void TMRB_PWM_SetForcePolarity(CM_TMRB_TypeDef *TMRBx,
                          uint16_t u16Polarity)
{
    /* Check parameters */
    DDL_ASSERT(IS_TMRB_UNIT(TMRBx));
    DDL_ASSERT(IS_TMRB_PWM_FORCE_POLARITY(u16Polarity));

    MODIFY_REG16(TMRBx->PCONR, TMRB_PCONR_FORC, u16Polarity);
}

/**
 * @}
 */

/**
 * @}
 */

#endif /* DDL_TMRB_ENABLE */

/**
 * @}
 */

/**
* @}
*/

/******************************************************************************
 * EOF (not truncated)
 *****************************************************************************/
